/** main.cpp
  * tomasulo pipelined processor
  * Author: Long Tran
  * Email:  ltran3@gatech.edu
  * Date created: Mar 19 2011
  */

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include "processor.h"

void process_parameter(int argc, char** argv, int &k0, int &k1, int &k2, int &D, int &m, int &N, std::string& fName);

int main(int argc, char** argv)
{
  std::cout << "Tomasulo pipelined processor ... simulating\n";

  int k0, k1, k2, D, m, N;
  std::string fName;
  std::ifstream f;

  process_parameter(argc, argv, k0, k1, k2, D, m, N, fName);
  if (!fName.empty())
    f.open(fName.c_str());

  int verbose = 0;
  Processor *p = fName.empty() ? new Processor(std::cin, k0, k1, k2, D, m, N, verbose) :
                                 new Processor(f,        k0, k1, k2, D, m, N, verbose);
  int ret = p->infiniteLoop();

  f.close();
  delete p;
  return ret;
}

void process_parameter(int argc, char** argv, int &k0, int &k1, int &k2, int &D, int &m, int &N, std::string& fName)
{
  if (argc < 7) {
    std::cout << "There should be at least 6 parameters: k0, k1, k2, d = D/(k0+k1+k2), m, N\n";
    std::cout << "Example: k* = 1, d = 1 (i.e. D = 3), m = 8, N = 4\n";
    std::cout << "  ./tom 1 1 1 1 8 4 ./gcc.txt\n";
    std::cout << "  ./tom 1 1 1 1 8 4 < ./gcc.txt\n";
    exit(0);
  }
  k0 = atoi(argv[1]);
  k1 = atoi(argv[2]);
  k2 = atoi(argv[3]);
  D  = atoi(argv[4])*(k0+k1+k2);
  m  = atoi(argv[5]);
  N  = atoi(argv[6]);
  if (!(k0 > 0 && k1 > 0 && k2 > 0 && D > 0 && m > 0 && N > 0)) {
    std::cout << "Check parameters: must satisfy k0 > 0 && k1 > 0 && k2 > 0 && d > 0 && m > 0 && N > 0\n";
    exit(0);
  }
  fName = argc >= 8 ? argv[7] : "";
}
